﻿
$(document).ready(function () {
    layui.use(['table', 'dropdown'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var dropdown = layui.dropdown;
        // 渲染表格的函数
        function renderTable(pageNum) {
            // 从查询框获取条件
            var FinanceTypeName = $("#FinanceTypeName").val();
            var Token = localStorage.getItem("token");
            // 发送Ajax请求
            $.ajax({
                url: '/Finance/GetFinanceType',  // 这里填上你的后端接口地址
                type: 'GET',
                data:
                {
                    Token: Token,
                    FinanceTypeName: FinanceTypeName  // 传递查询条件
                },
                dataType: 'json',
                success: function (response)
                {
                    var data = JSON.parse(response)
                    // 渲染表格
                    table.render({
                        elem: '#dataTable',
                        cols: [[
                            { type: 'checkbox', fixed: 'left' },
                            { field: 'financeTypeCode', title: '类别编号',width:235,align: 'center' },
                            { field: 'financeTypeName', title: '类别名称', width: 240, align: 'center' },
                            { field: 'addTime', title: '创建时间', width: 500, align: 'center', templet: '<div>{{ layui.util.toDateString(d.addTime, "yyyy-MM-dd HH:mm:ss") }}</div>', },
                            //操作列
                            {
                                fixed: 'right', width: 300, title: '操作', align:'center', templet: function (d)
                                {
                                    return '<a class="layui-btn layui-btn-xs layui-icon layui-icon-edit" lay-on="test-page-custom" onclick="UpdFinance(' + d.financeTypeId + ')">编辑</a><button class="layui-btn layui-btn-xs layui-icon layui-icon-delete"  lay - event="moredel" onclick="DelFinance(' + d.financeTypeId + ')">删除</a > '
                                },
                            }
                        ]],
                        data: data.data,  // 数据源
                        toolbar: '#toolbarDemo',
                        defaultToolbar: ['exports'],
                    });
                },
                error: function (xhr, status, error) {
                    console.log('请求失败: ' + error);
                }
            });

            ////////////选择复选框之后的数据////////////////////////////////////////////////
            document.getElementById('getCheckData').onclick = function () {
                // 获取所有勾选的行数据
                var checkedData = table.checkStatus('dataTable').data;
                // 提取选中数据的ID
                var checkedIds = checkedData.map(function (item) {
                    //console.log(item)
                    return item.financeTypeId;  // 获取每一行的id
                });
                // 输出选中的ID
                //console.log(checkedIds);
                var currentDate = new Date();
                var deldto = {
                    financeTypeId: checkedIds,
                    delName: "朱泊霖",
                    delTime: currentDate.getFullYear() + '-' + (currentDate.getMonth() + 1) + '-' + currentDate.getDate() + ' ' + currentDate.getHours() + ':' + currentDate.getMinutes(),
                    Token: localStorage.getItem("token")
                }
                //console.log(deldto);
                if (deldto.financeTypeId.length == 0) {
                    layer.msg('批量删除至少选择一项');
                    return
                }
                layer.confirm('你真的要批量删除吗？', { icon: 3 }, function () {
                    $.ajax({
                        url: "/Finance/DelFinanceType",
                        data: deldto,
                        type: "post",
                        success: function (res) {
                            //console.log(res)
                            var s = JSON.parse(res)
                            if (s.data > 0) {
                                layer.msg('批量删除成功', { icon: 0 }, function () {
                                    //layer.msg('提示框关闭后的回调');
                                    location.reload()
                                });
                            }
                            else {
                                layer.msg('批量删除失败');
                                return
                            }
                        }
                    })
                }, function () {
                    layer.msg('取消批量删除');
                })
            }


            ////////////选择复选框之后的数据////////////////////////////////////////////////
            document.getElementById('piUpd').onclick = function () {
                // 获取所有勾选的行数据
                var checkedData = table.checkStatus('dataTable').data;
                // 提取选中数据的ID
                var checkedIds = checkedData.map(function (item) {
                    //console.log(item)
                    return item.financeTypeId;  // 获取每一行的id
                });
                console.log(checkedIds);
                if (checkedIds.length == 0) {
                    layer.msg('编辑至少选择一项');
                    return
                }
                if (checkedIds.length >= 2) {
                    layer.msg('编辑只能选择一项');
                    return
                }
                var obj = {
                    financeTypeId: '',
                    FinanceTypeCode: '',
                    financeTypeName: '',
                    fRremark: '',
                    office_Id: '',
                    user_Id: '',
                    addName: '',
                    addTime: '',
                    updName: '',
                    updTime: '',
                    delName: '',
                    delTime: ''
                }
                    //反填方法
                    $.ajax({
                        url: "../Finance/FanFinanceType",
                        data: {
                            token: localStorage.getItem("token"),
                            id: checkedIds[0]
                        },
                        type: "get",
                        success: function (res) {
                            var data = JSON.parse(res)
                            $(data.data).each(function () {
                                $("#financeTypeId").val(this.financeTypeId)

                                $("#FinanceTypeCode").val(this.financeTypeCode)
                                $("#financeTypeName").val(this.financeTypeName)
                                $("#fRremark").val(this.fRremark)

                                $("#office_Id").val(this.office_Id)
                                $("#user_Id").val(this.user_Id)
                                $("#addName").val(this.addName)
                                $("#addTime").val(this.addTime)
                                $("#updName").val(this.updName)
                                $("#updTime").val(this.updTime)
                                $("#delName").val(this.delName)
                                $("#delTime").val(this.delTime)
                                var currentDate = new Date();
                                obj.financeTypeId = this.financeTypeId;
                                obj.FinanceTypeCode = this.financeTypeCode;
                                obj.financeTypeName = this.financeTypeName;
                                obj.fRremark = this.fRremark;
                                obj.office_Id = this.office_Id;
                                obj.user_Id = this.user_Id;
                                obj.addName = this.addName;
                                obj.addTime = this.addTime;
                                obj.updName = '卢芳';

                                obj.updTime = currentDate.getFullYear() + '-' + (currentDate.getMonth() + 1) + '-' + currentDate.getDate() + ' ' + currentDate.getHours() + ':' + currentDate.getMinutes();
                                obj.delName = this.delName;
                                obj.delTime = this.delTime;
                            })
                        }
                    })
                    //打开弹框
                    layer.open({
                        type: 1,
                        area: '350px',
                        resize: false,
                        shadeClose: true,
                        title: '修改类别',
                        content: `<form class="layui-form" action="">
                                <div class="layui-form" lay-filter="filter-test-layer" style="margin: 16px;">
                                    <div class="demo-login-container">
                                       <div class="layui-form-item">
                                         <div class="layui-input-wrap">
                                             类型编号： <input type="text" id="FinanceTypeCode" name="FinanceTypeCode" value="" placeholder="可不填写,自动生成" autocomplete="off" class="layui-input" lay-affix="clear">
                                         </div>
                                       </div>
                                       <div class="layui-form-item">
                                             <div class="layui-input-wrap">
                                                 类型名称：<input type="text" id="financeTypeName" lay-verify="required"  name="financeTypeName" value="" lay-verify="required" placeholder="全部" autocomplete="off" class="layui-input" lay-affix="clear">
                                             </div>
                                       </div>
                                        <div class="layui-form-item">
                                             <div class="layui-input-wrap">
                                                备注：<input type="text" value="" id="fRremark" name="FRremark" lay-verify="required" placeholder="全部" lay-reqtext="请填写备注" autocomplete="off" class="layui-input" lay-affix="clear">
                                             </div>
                                        </div>
                                        <div class="layui-form-item">
                                            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="demo-logins">确定</button>
                                        </div>
                                    </div>
                               </div>
                            </form>`,
                        success: function () {
                            var obj1 = {
                                financeTypeId: '',
                                FinanceTypeCode: '',
                                financeTypeName: '',
                                FRremark: '',
                                office_Id: '',
                                user_Id: '',
                                addName: '',
                                addTime: '',
                                updName: '',
                                updTime: '',
                                Token: ''
                            };
                            // 对弹层中的表单进行初始化渲染
                            form.render();
                            // 表单提交事件
                            form.on('submit(demo-logins)', function (data) {
                                var field = data.field; // 获取表单字段值
                                //赋值
                                obj1.financeTypeId = obj.financeTypeId;
                                obj1.FinanceTypeCode = field.FinanceTypeCode;
                                obj1.financeTypeName = field.financeTypeName;
                                obj1.FRremark = field.FRremark;
                                obj1.office_Id = obj.office_Id;
                                obj1.user_Id = obj.user_Id;
                                obj1.addName = obj.addName;
                                obj1.addTime = obj.addTime;
                                obj1.updName = obj.updName;
                                obj1.updTime = obj.updTime;
                                obj1.Token = localStorage.getItem("token");
                                console.log(obj1)
                                //修改
                                $.ajax({
                                    url: "/Finance/UpdFinanceType",
                                    data: obj1,
                                    type: "post",
                                    success: function (res) {
                                        var res1 = JSON.parse(res)
                                        // 请求成功时的回调函数
                                        console.log('Response:', res);
                                        if (res1.data == 1) {
                                            layer.msg('修改成功', { icon: 0 }, function () {
                                                // layer.msg('提示框关闭后的回调');
                                                location.reload()
                                            });
                                        }
                                        else if (res1.data == -1) {
                                            layer.msg('类别名称已存在，不能修改');
                                            return
                                        }
                                        else {
                                            layer.msg('修改失败');
                                            return
                                        }
                                    }
                                })


                                return false; // 阻止默认 form 跳转
                            })
                        },

                    })
















            }























        }
        // 监听全选操作
        form.on('checkbox(check-all)', function (data) {
            var checked = data.elem.checked;  // 获取全选复选框的状态
            table.checkStatus('dataTable').isAll = checked;  // 设置是否全选
            form.render('checkbox');  // 重新渲染表单
        });

        ////////////修改////////////////////////////////////////////////
        var obj = {
            financeTypeId: '',
            FinanceTypeCode: '',
            financeTypeName: '',
            fRremark: '',
            office_Id: '',
            user_Id: '',
            addName: '',
            addTime: '',
            updName: '',
            updTime: '',
            delName: '',
            delTime: ''
        }
        window.UpdFinance = function (d) {
            //反填方法
            $.ajax({
                url: "../Finance/FanFinanceType",
                data: {
                    token: localStorage.getItem("token"),
                    id: JSON.parse(d) 
                },
                type: "get",
                success: function (res) {
                    var data = JSON.parse(res)
                    $(data.data).each(function () {
                        $("#financeTypeId").val(this.financeTypeId)

                        $("#FinanceTypeCode").val(this.financeTypeCode)
                        $("#financeTypeName").val(this.financeTypeName)
                        $("#fRremark").val(this.fRremark)

                        $("#office_Id").val(this.office_Id)
                        $("#user_Id").val(this.user_Id)
                        $("#addName").val(this.addName)
                        $("#addTime").val(this.addTime)
                        $("#updName").val(this.updName)
                        $("#updTime").val(this.updTime)
                        $("#delName").val(this.delName)
                        $("#delTime").val(this.delTime)
                        var currentDate = new Date();
                        obj.financeTypeId = this.financeTypeId;
                        obj.FinanceTypeCode = this.financeTypeCode;
                        obj.financeTypeName = this.financeTypeName;
                        obj.fRremark = this.fRremark;
                        obj.office_Id = this.office_Id;
                        obj.user_Id = this.user_Id;
                        obj.addName = this.addName;
                        obj.addTime = this.addTime;
                        obj.updName = '卢芳';

                        obj.updTime = currentDate.getFullYear() + '-' + (currentDate.getMonth() + 1) + '-' + currentDate.getDate() + ' ' + currentDate.getHours() + ':' + currentDate.getMinutes();
                        obj.delName = this.delName;
                        obj.delTime = this.delTime;
                    })
                }
            })
            //打开弹框
            layer.open({
                type: 1,
                area: '350px',
                resize: false,
                shadeClose: true,
                title: '修改类别',
                content: `<form class="layui-form" action="">
                                <div class="layui-form" lay-filter="filter-test-layer" style="margin: 16px;">
                                    <div class="demo-login-container">
                                       <div class="layui-form-item">
                                         <div class="layui-input-wrap">
                                             类型编号： <input type="text" id="FinanceTypeCode" name="FinanceTypeCode" value="" placeholder="可不填写,自动生成" autocomplete="off" class="layui-input" lay-affix="clear">
                                         </div>
                                       </div>
                                       <div class="layui-form-item">
                                             <div class="layui-input-wrap">
                                                 类型名称：<input type="text" id="financeTypeName" lay-verify="required"  name="financeTypeName" value="" lay-verify="required" placeholder="全部" autocomplete="off" class="layui-input" lay-affix="clear">
                                             </div>
                                       </div>
                                        <div class="layui-form-item">
                                             <div class="layui-input-wrap">
                                                备注：<input type="text" value="" id="fRremark" name="FRremark" lay-verify="required" placeholder="全部" lay-reqtext="请填写备注" autocomplete="off" class="layui-input" lay-affix="clear">
                                             </div>
                                        </div>
                                        <div class="layui-form-item">
                                            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="demo-logins">确定</button>
                                        </div>
                                    </div>
                               </div>
                            </form>`,
                success: function () {
                    var obj1 = {
                        financeTypeId: '',
                        FinanceTypeCode: '',
                        financeTypeName: '',
                        FRremark: '',
                        office_Id: '',
                        user_Id: '',
                        addName: '',
                        addTime: '',
                        updName: '',
                        updTime: '',
                        Token:''
                    };
                    // 对弹层中的表单进行初始化渲染
                    form.render();
                    // 表单提交事件
                    form.on('submit(demo-logins)', function (data) {
                        var field = data.field; // 获取表单字段值
                        //赋值
                        obj1.financeTypeId = obj.financeTypeId;
                        obj1.FinanceTypeCode = field.FinanceTypeCode;
                        obj1.financeTypeName = field.financeTypeName;
                        obj1.FRremark = field.FRremark;
                        obj1.office_Id = obj.office_Id;
                        obj1.user_Id = obj.user_Id;
                        obj1.addName = obj.addName;
                        obj1.addTime = obj.addTime;
                        obj1.updName = obj.updName;
                        obj1.updTime = obj.updTime;
                        obj1.Token = localStorage.getItem("token");
                        console.log(obj1)
                        //修改
                        $.ajax({
                            url: "/Finance/UpdFinanceType",
                            data: obj1,
                            type: "post",
                            success: function (res) {
                                var res1 = JSON.parse(res)
                                // 请求成功时的回调函数
                                console.log('Response:', res);
                                if (res1.data == 1) {
                                    layer.msg('修改成功', { icon: 0 }, function () {
                                        // layer.msg('提示框关闭后的回调');
                                        location.reload()
                                    });
                                }
                                else if (res1.data == -1) {
                                    layer.msg('类别名称已存在，不能修改');
                                    return
                                }
                                else {
                                    layer.msg('修改失败');
                                    return
                                }
                            }
                        })
                        
                       
                        return false; // 阻止默认 form 跳转
                    })
                },
                
            })
        }

        ////////////删除////////////////////////////////////////////////
        window.DelFinance = function (id) {
            var currentDate = new Date();
            var delres = {
                financeTypeId: id,
                delName: '朱泊霖',
                delTime: currentDate.getFullYear() + '-' + (currentDate.getMonth() + 1) + '-' + currentDate.getDate() + ' ' + currentDate.getHours() + ':' + currentDate.getMinutes(),
                Token: localStorage.getItem("token")
            };
            layer.confirm('你真的要删除吗？', { icon: 3 }, function () {
                $.ajax({
                    url: "/Finance/DelFinanceType",
                    data: delres,
                    type: "post",
                    success: function (daa) {
                        var s = JSON.parse(daa)
                        if (s.data == 1) {
                            layer.msg('删除成功', { icon: 0 }, function () {
                                location.reload()
                            });
                        }
                        else {
                            layer.msg('删除失败');
                            return
                        }
                    }
                })
            }, function () {
                layer.msg('取消删除');
            });





        }


        // 初次加载表格数据
        renderTable(1);

        // 查询按钮点击事件
        $('#searchBtn').click(function () {
            renderTable(1);  // 重置为第一页，并重新加载数据
        });

        // 获取选中数据按钮
        $('#getSelected').click(function () {
            var checkStatus = table.checkStatus('dataTable');
            var selectedData = checkStatus.data;  // 获取选中的数据
            console.log(selectedData);  // 打印选中的数据
        });

        ////////////新增//////////////////////////////////////////////////
        layui.use(function () {
            var $ = layui.$;
            var layer = layui.layer;
            var util = layui.util;
            var form = layui.form;
            // 事件
            util.on('lay-on', {
                'test-page-custom1': function () {
                    layer.open({
                        type: 1,
                        area: '350px',
                        resize: false,
                        shadeClose: true,
                        title: '新增财务类别',
                        content: `
                            <form class="layui-form" action="">
                                <div class="layui-form" lay-filter="filter-test-layer" style="margin: 16px;">
                                    <div class="demo-login-container">
                                       <div class="layui-form-item">
                                         <div class="layui-input-wrap">
                                             类型编号： <input type="text" name="FinanceTypeCode" value="" placeholder="可不填写,自动生成" autocomplete="off" class="layui-input" lay-affix="clear">
                                         </div>
                                       </div>
                                       <div class="layui-form-item">
                                             <div class="layui-input-wrap">
                                                 类型名称：<input type="text" lay-verify="required"  name="financeTypeName" value="" lay-verify="required" placeholder="全部" autocomplete="off" class="layui-input" lay-affix="clear">
                                             </div>
                                       </div>
                                        <div class="layui-form-item">
                                             <div class="layui-input-wrap">
                                                备注：<input type="text" value="" name="FRremark" lay-verify="required" placeholder="全部" lay-reqtext="请填写备注" autocomplete="off" class="layui-input" lay-affix="clear">
                                             </div>
                                        </div>
                                        <div class="layui-form-item">
                                            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="demo-login1">添加</button>
                                        </div>
                                    </div>
                               </div>
                            </form>`,
                        success: function () {
                            // 对弹层中的表单进行初始化渲染
                            form.render();
                            // 表单提交事件
                            var datas = {
                                financeTypeId: '',
                                financeTypeCode: '',
                                financeTypeName: '',
                                fRremark: '',
                                office_Id: '',
                                user_Id: '',
                                addName: '',
                                addTime: '',
                                updName: '',
                                updTime: '',
                                delName: '',
                                delTime: '',
                                Token:''
                            };
                            form.on('submit(demo-login1)', function (dates) {
                                var field = dates.field; // 获取表单字段值
                                // 生成6位数
                                var randomNum = Math.floor(100000 + Math.random() * 900000);  // 生成100000到999999之间的随机整数
                                datas.financeTypeCode = randomNum;
                                datas.financeTypeName = field.financeTypeName;
                                datas.fRremark = field.FRremark;
                                datas.office_Id = '1';
                                datas.user_Id = 1;
                                datas.addName = '张三';
                                var times = new Date();
                                datas.addTime = times.getFullYear() + '-' + (times.getMonth() + 1) + '-' + times.getDate() + ' ' + times.getHours() + ':' + times.getMinutes();
                                datas.Token = localStorage.getItem("token");
                                $.ajax({
                                    url: "/Finance/AddFinanceType",
                                    data: {
                                        dto: datas
                                    },
                                    type: "post",
                                    success: function (res) {
                                        var res1 = JSON.parse(res)
                                        // 请求成功时的回调函数
                                        //console.log('Response:', res);
                                        if (res1.data == 1) {
                                            layer.msg('添加成功', { icon: 0 }, function () {
                                                location.reload()
                                            });
                                        }
                                        else if (res1.data == -1) {
                                            layer.msg('类别名称已存在，不能添加');
                                            return
                                        }
                                        else {
                                            layer.msg('添加失败');
                                            return
                                        }
                                    }
                                })
                                return false; // 阻止默认 form 跳转
                            });
                        }
                    });
                }
            })
        });





















    });
});